package com.nwpu.am.utils;

import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;

/**
 * MD5Util
 * <p>
 * md加密
 *
 * @author Roy
 * @version 1.0
 * {@code @date} 2024/6/3
 */

public class MD5Util {
    private MD5Util() {}

    /**
     * 获取md5加密内容
     *
     * @param inStr
     * @return
     */
    public static String getMD5AsHex(String inStr) {
        return DigestUtils.md5DigestAsHex(inStr.getBytes(StandardCharsets.UTF_8));
    }

    /**
     * 获取md5加盐加密内容
     *
     * @param inStr
     * @param salt 盐值
     * @param iterations 迭代次数
     * @return
     */
    public static String getMD5AsHexWithSalt(String inStr, String salt, int iterations) {
        String result = inStr + salt;
        for (int i = 0; i < iterations; i++) {
            result = DigestUtils.md5DigestAsHex(result.getBytes(StandardCharsets.UTF_8));
        }
        return result;
    }

}